package com.ostrich.cloud.ostrichUserService.app.controller;



import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ostrich.cloud.common.result.PageResult;
import com.ostrich.cloud.common.result.Result;
import com.ostrich.cloud.ostrichUserService.app.config.JwtHelper;
import com.ostrich.cloud.ostrichUserService.app.utils.GetParentId;
import com.ostrich.cloud.ostrichUserService.client.BusinessmanMangerClient;
import com.ostrich.cloud.ostrichUserService.infra.model.Businessman;
import com.ostrich.cloud.ostrichUserService.infra.model.UserParent;
import com.ostrich.cloud.ostrichUserService.service.BusinessmanService;
import com.ostrich.ostrichUserService.dto.bussiness.BusinessmanDto;
import com.ostrich.ostrichUserService.param.bussiness.LoginBusParam;
import com.ostrich.ostrichUserService.param.bussiness.RegBussinessmanParam;
import com.ostrich.ostrichUserService.param.bussiness.UpdateBusParam;
import org.apache.commons.lang.time.DateUtils;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 商户表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2022-10-26
 */
@RestController
public class BusinessmanController implements BusinessmanMangerClient {

    @Resource
    private BusinessmanService businessmanService;

    @Resource
    private JwtHelper jwtHelper;

    @Override
    public Result addBusiness(RegBussinessmanParam regBus) {

        businessmanService.addBusiness(regBus);
        return Result.okm("注册成功");
    }

    @Override
    public Result updateBusState(Integer parentId, String status) {

        businessmanService.updateBusState(parentId,status);
        return Result.okm("更新成功");
    }

    @Override
    public Result businessmanLogin(LoginBusParam loginBusParam) {
        UserParent userParent = businessmanService.businessmanLogin(loginBusParam);
        Date date = DateUtils.addDays(new Date(), 1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("operatorId",userParent.getParentId());
        map.put("operatorName","businessman");
        String jwt = jwtHelper.createJWT(date, map);
       //  第一步：比对前端提交的账号名称、密码
       return Result.ok(jwt);

    }

    @Override
    public Result getAllBusiness(Integer current, Integer size, String status) {
        PageResult<BusinessmanDto> allBusiness = businessmanService.getAllBusiness(current, size, status);
        return Result.ok(allBusiness);
    }

    @Override

    public Result updateBusinessMessage(UpdateBusParam updateBusParam) {

        Businessman businessman = BeanUtil.copyProperties(updateBusParam, Businessman.class);


         Integer parentId = GetParentId.getParentId();
         businessman.setParentId(parentId);

        businessmanService.updateBusinessMessage(businessman);
        return Result.okm("修改完成");
    }

    @Override
    public Result getAllBusinessByLocation(String latitude, String logitude,Double radius) {
        Map<Double, Businessman> allBusinessByLocation = businessmanService.getAllBusinessByLocation(latitude, logitude, radius);
        return Result.ok(allBusinessByLocation);
    }

    @Override
    public Result getOneBusinessByLocation(String latitude, String logitud) {
        Map<Double, Businessman> oneBusinessByLocation = businessmanService.getOneBusinessByLocation(latitude, logitud);
        return Result.ok(oneBusinessByLocation);
    }

    @Override
    public Result getOnlyBusiness() {

        Integer parentId = GetParentId.getParentId();
        Businessman businessman = businessmanService.getOne(new QueryWrapper<Businessman>().eq("parent_id", parentId));
        BusinessmanDto businessmanDto = BeanUtil.copyProperties(businessman, BusinessmanDto.class);
        return Result.ok(businessmanDto);

    }
}

